package com.mark.springbootredis.service.impl;

import com.mark.springbootredis.entity.Admin;
import com.mark.springbootredis.mapper.AdminMapper;
import com.mark.springbootredis.request.LoginRequest;
import com.mark.springbootredis.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: MARK
 * @Date: 2019/8/20 08:13
 * @Version: 1.0.0
 * @Description:
 */
@Service
@Slf4j
public class AdminServiceImpl implements AdminService {
    @Resource
    private AdminMapper adminMapper;

    @Override
    public List<Admin> findAll() {
        List<Admin> admins = adminMapper.selectAll();
        return admins;
    }

    @Override
    public boolean login(LoginRequest loginRequest) {
        Example example = new Example(Admin.class);
        example.and().andEqualTo("username", loginRequest.getUsername());
        Admin admin1 = adminMapper.selectOneByExample(example);
        if(admin1 == null){
            return false;
        }else{
            if(getByUsername(loginRequest.getUsername())){
                return loginRequest.getPassword().equals(admin1.getPassword());
            }
            return false;
        }
    }
    //判断是否有该用户名
    @Override
    public boolean getByUsername(String username) {
        Example example = new Example(Admin.class);
        example.and().andEqualTo("username", username);
        Admin admin = adminMapper.selectOneByExample(example);
        //返回true 已经有该用户名
        return admin != null;
    }
}
